package com.young.shop.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author LY
 * @date 2020/12/16 9:43
 */
public class ResponseUtils {
    public static final String CHARACTER_ENCODING = "utf-8";
    //作为请求头告诉服务端消息主体是序列化的JSON字符串
    public static final String CONTENT_TYPE = "application/json; charset=utf-8";
    public static void responseToJson(HttpServletResponse response, Object object) throws IOException {
        //GET POST给前台传数据
        response.setCharacterEncoding(CHARACTER_ENCODING);
        response.setContentType(CONTENT_TYPE);
        response.getWriter().println(JSONObject.toJSONString(object));
}
}
